$(function () {
    $("#jqGrid").Grid({
        url: '../materialrecord/list?delFlag=1&isReplaceable=1',
        colModel: [
            {label: '备件编码', name: 'spareCode', align: 'center', index: 'SPARE_CODE', width: 80},
            {label: '备件名称', name: 'spareName', align: 'center', index: 'SPARE_NAME', width: 100},
            {label: '备件规格', name: 'spec', align: 'center',index: 'spec', width: 100},
            {label: '领料单号', name: 'material', align: 'center', index: 'MATEROAL', width: 130},
            {label: '关联工单号', name: 'jobCode', align: 'center', index: 'JOB_CODE', width: 130},
            {label: '领用部门', name: 'deptName', align: 'center', index: 'DEPT_NAME', width: 120},
            {label: '申请人', name: 'pickerName', align: 'center', index: 'PICKER_NAME', width: 80},
            {
                label: '申请时间', name: 'pickDate', align: 'center', index: 'PICK_DATE', width: 120,
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm');
                }
            },
            {label: '用途', name: 'purpose', align: 'center', index: 'PURPOSE', width: 120},
            {label: '仓库名称', name: 'storeName', align: 'center', index: 'STORE_NAME', width: 230},
            {label: '备件位置', name: 'postion', align: 'center', index: 'POSTION', width:150},
            {label: '领用数', name: 'actualNum', align: 'center', index: 'ACTUAL_NUM', width: 50},
            {label: '更换数', name: 'replaceNum', align: 'center', index: 'TRPLACE_NUM', width: 50},
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
        ]
    });
    // 获取车间列表
    vm.initUserDept();
});
var ztree;
var deptSetting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    },
    callback: {
        beforeCheck: true,
        beforeClick: function (e, treeObj, treeNode) {
            vm.q.deptId = treeObj.deptId;
            vm.reload();
        }
    }
};

let vm = new Vue({
    el: '#rrapp',
    data: {
        treeList: [], // 车间列表
        q: {
            deptId: '',//选中的id
            partCode:'',//备件编码
            spareName:'',//备件名称
            spec:'',//规格型号
            erpCode:''//erp编码
        },
    },
    methods: {
        // 加载车间树状结构
        initUserDept: function () {
            Ajax.request({
                url: '../sys/user/treeDeptBySCXLevel?level=2',
                async: false,
                successCallback: function (r) {
                    vm.treeList = r.list;
                    deptZtree = $.fn.zTree.init($("#storeTree"), deptSetting, vm.treeList);
                    deptZtree.expandAll(true);
                }
            });
        },
        query: function () {
            vm.reload();
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                search: true,
                postData: vm.q,
                page: page
            }).trigger("reloadGrid", [{page: 1}]);
        },
        // 导出设备基础信息
        exportData: function () {
            // 查询条件数组
            let queryConditionArray = [];
            if (this.q.deptId) {
                queryConditionArray.push('deptId=' + this.q.deptId);
            }
            if (this.q.partCode) {
                queryConditionArray.push('partCode=' + this.q.partCode);
            }
            let queryCondition = '';
            if(queryConditionArray.length > 0){
                queryCondition += '?' + queryConditionArray.join('&');
            }
            window.open('../materialrecord/exportData' + queryCondition, '_blank');
        },
        reloadSearch: function () {
            vm.q = {
                deptId: '',
                partCode:''
            };
            vm.reload();
        }
    }
});
